package com.xyz.keshe;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xyz.keshe.adapter.AnnouncementAdapter;
import com.xyz.keshe.adapter.CategoryAdapter;
import com.xyz.keshe.adapter.NewsAdapter;
import com.xyz.keshe.dao.AnnouncementDao;
import com.xyz.keshe.dao.CategoryDao;
import com.xyz.keshe.dao.NewsDao;
import com.xyz.keshe.model.Announcement;
import com.xyz.keshe.model.Category;
import com.xyz.keshe.model.News;

import java.util.List;

public class UserHomeActivity extends AppCompatActivity {

    private RecyclerView rvCategories;
    private EditText etSearch;
    private ImageButton btnSearch;
    private ListView lvNews;
    private ListView lvAnnouncements;
    private LinearLayout llNoData;

    private CategoryDao categoryDao;
    private NewsDao newsDao;
    private AnnouncementDao announcementDao;

    private CategoryAdapter categoryAdapter;
    private NewsAdapter newsAdapter;
    private AnnouncementAdapter announcementAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_home);

        // 初始化视图
        rvCategories = findViewById(R.id.rv_categories);
        etSearch = findViewById(R.id.et_search);
        btnSearch = findViewById(R.id.btn_search);
        lvNews = findViewById(R.id.lv_news);
        lvAnnouncements = findViewById(R.id.lv_announcements);
        llNoData = findViewById(R.id.ll_no_data);

        // 初始化DAO
        categoryDao = new CategoryDao(this);
        newsDao = new NewsDao(this);
        announcementDao = new AnnouncementDao(this);

        // 设置栏目列表
        rvCategories.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        List<Category> categories = categoryDao.getAllCategories();
        categoryAdapter = new CategoryAdapter(this, categories);
        rvCategories.setAdapter(categoryAdapter);

        // 栏目点击事件
        categoryAdapter.setOnItemClickListener(new CategoryAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Category category) {
                // 跳转到栏目详情页
                Intent intent = new Intent(UserHomeActivity.this, CategoryDetailActivity.class);
                intent.putExtra("category_id", category.getId());
                intent.putExtra("category_name", category.getName());
                startActivity(intent);
            }
        });

        // 设置首页新闻列表（显示每个栏目最新的3条新闻）
        List<News> latestNews = newsDao.getLatestNewsByCategory(3);
        newsAdapter = new NewsAdapter(this, latestNews);
        lvNews.setAdapter(newsAdapter);

        // 新闻点击事件
        lvNews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                News news = (News) parent.getItemAtPosition(position);
                // 跳转到新闻详情页
                Intent intent = new Intent(UserHomeActivity.this, NewsDetailActivity.class);
                intent.putExtra("news_id", news.getId());
                startActivity(intent);
            }
        });

        // 设置公告列表
        List<Announcement> announcements = announcementDao.getAllAnnouncements();
        announcementAdapter = new AnnouncementAdapter(this, announcements);
        lvAnnouncements.setAdapter(announcementAdapter);

        // 公告点击事件
        announcementAdapter.setOnAnnouncementClickListener(new AnnouncementAdapter.OnAnnouncementClickListener() {
            @Override
            public void onAnnouncementClick(Announcement announcement) {
                // 跳转到公告详情页
                Intent intent = new Intent(UserHomeActivity.this, AnnouncementDetailActivity.class);
                intent.putExtra("announcement_id", announcement.getId());
                startActivity(intent);
            }
        });

        // 搜索按钮点击事件
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                search();
            }
        });

        // 显示或隐藏无数据提示
        if (latestNews.isEmpty()) {
            llNoData.setVisibility(View.VISIBLE);
            lvNews.setVisibility(View.GONE);
        } else {
            llNoData.setVisibility(View.GONE);
            lvNews.setVisibility(View.VISIBLE);
        }
    }

    private void search() {
        String keyword = etSearch.getText().toString().trim();
        if (TextUtils.isEmpty(keyword)) {
            return;
        }

        // 跳转到搜索结果页
        Intent intent = new Intent(this, SearchResultActivity.class);
        intent.putExtra("keyword", keyword);
        startActivity(intent);
    }
} 